package com.tsd.jxc.fin.service.impl;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.fin.dao.JxcFinanceAccountMapper;
import com.tsd.jxc.fin.entity.JxcFinanceAccount;
import com.tsd.jxc.fin.entity.JxcFinanceAccountExt;
import com.tsd.jxc.fin.service.JxcFinanceAccountService;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcFinanceAccount 服务实现层
 *
 * @author Hillpool
 * @date 2022/09/01 16:14:21
 */
@Service
public class JxcFinanceAccountServiceImpl extends BaseServiceImpl implements JxcFinanceAccountService {

    @Resource
    private JxcFinanceAccountMapper jxcFinanceAccountMapper;

    @Override
    public JxcFinanceAccount getJxcFinanceAccountById(Long id) throws Exception {
        return jxcFinanceAccountMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcFinanceAccountExt queryBySid(String sid) throws Exception {
        return jxcFinanceAccountMapper.selectBySid(sid);
    }

    @Override
    public void deleteJxcFinanceAccounts(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            JxcFinanceAccount old = jxcFinanceAccountMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            jxcFinanceAccountMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveJxcFinanceAccount(JxcFinanceAccount record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            jxcFinanceAccountMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            jxcFinanceAccountMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<JxcFinanceAccountExt> findJxcFinanceAccounts(Map<String, Object> params, PageBean page) throws Exception {
        return jxcFinanceAccountMapper.selectByPage(params, page);
    }

    @Override
    public List<JxcFinanceAccountExt> findJxcFinanceAccounts(Map<String, Object> params) throws Exception {
        return jxcFinanceAccountMapper.selectByParams(params);
    }

    @Override
    public void initData(String related_party_sid, SysUser opUser) throws Exception {
        JxcFinanceAccountExt accountExt = new JxcFinanceAccountExt();
        accountExt.setRelated_party_sid(related_party_sid);
        accountExt.setAmount(BigDecimal.ZERO);
        saveJxcFinanceAccount(accountExt, opUser);
    }
}